<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover" />
    <title>分享好友</title>
    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
    <script src="js/jq.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .share {
            width: 100%;
            height: 100vh;
            background: rgb(254, 121, 2);
        }

        .img {
            width: 100%;
        }
    </style>
</head>

<body>
    <div class='share'>
        <img class='img' src="./img/fen.png" alt="">
    </div>
    <script>
        let u = navigator.userAgent;
        let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端	
        let regName = getUrldata('Uid')

        function getUrldata(name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]);
            return null;
        }

        $(() => {
            $.ajax({
                type: 'POST',
                url: 'https://api.cd1a.cn/index.php/activity/power5/InsertInvitation1',
                data: {
                    user_id: regName
                },
                dataType: 'json',
                async: true,
                success: function (res) {
                    console.log(res)
                    //无用户信息
                    if (res.code == 402) {
                        if (isWeiXin()) {
                            (function () {
                                window.alert = function (name) {
                                    var iframe = document.createElement("IFRAME");
                                    iframe.style.display = "none";
                                    iframe.setAttribute("src", 'data:text/plain');
                                    document.documentElement.appendChild(iframe);
                                    window.frames[0].window.alert(name);
                                    iframe.parentNode.removeChild(iframe);
                                }
                            })();
                            alert('没有您的用户信息，请先前往小程序登录');
                            window.location.href = 'javascript:history.go(-1)'
                            return
                        }
                        if (isiOS) {
                            //ios
                            window.webkit.messageHandlers.showAlert.postMessage('没有您的用户信息');
                            window.location.href = 'javascript:history.go(-1)'
                            return
                        }
                        if (isAndroid) {
                            window.TiantianCharge_Android.showAlert('没有您的用户信息');
                            window.location.href = 'javascript:history.go(-1)'
                            return
                        }
                    }
                    $('.img').click(function () {
                        // 判断小程序环境
                        if (isWeiXin()) {
                            if (window.__wxjs_environment === 'miniprogram') {
                                $.ajax({
                                    type: 'POST',
                                    url: 'https://api.cd1a.cn/index.php/activity/swap4/downloadImg',
                                    data: {
                                        user_id: regName,
                                        imgurl: res.data.z_url1
                                    },
                                    success: function (res) {
                                        window.location.href = 'javascript:history.go(-1)'
                                    }
                                })
                            }
                        }
                        if (isiOS) {
                            window.webkit.messageHandlers.share.postMessage({
                                url: res.data.z_url1
                            })
                            return
                        }
                        if (isAndroid) {
                            //Android
                            var zu = res.data.z_url1
                            window.TiantianCharge_Android.share(zu)
                            return
                        }
                    })
                }
            })
        })

        //判断微信
        function isWeiXin() {
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                return true; // 是微信端
            } else {
                return false;
            }
        }
    </script>
</body>

</html>